{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![AWS SDK for pandas](_static/logo.png \"AWS SDK for pandas\")](https://github.com/aws/aws-sdk-pandas)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 30 - Data Api"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Data Api simplifies access to Amazon Redshift and RDS by removing the need to manage database connections and credentials. Instead, you can execute SQL commands to an Amazon Redshift cluster or Amazon Aurora cluster by simply invoking an HTTPS API endpoint provided by the Data API. It takes care of managing database connections and returning data. Since the Data API leverages IAM user credentials or database credentials stored in AWS Secrets Manager, you don’t need to pass credentials in API calls."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Connect to the cluster\n",
    "- [wr.data_api.redshift.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.data_api.redshift.connect.html)\n",
    "- [wr.data_api.rds.connect()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.data_api.rds.connect.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "con_redshift = wr.data_api.redshift.connect(\n",
    "    cluster_id=\"aws-sdk-pandas-1xn5lqxrdxrv3\",\n",
    "    database=\"test_redshift\",\n",
    "    secret_arn=\"arn:aws:secretsmanager:us-east-1:111111111111:secret:aws-sdk-pandas/redshift-ewn43d\",\n",
    ")\n",
    "\n",
    "con_redshift_serverless = wr.data_api.redshift.connect(\n",
    "    workgroup_name=\"aws-sdk-pandas\",\n",
    "    database=\"test_redshift\",\n",
    "    secret_arn=\"arn:aws:secretsmanager:us-east-1:111111111111:secret:aws-sdk-pandas/redshift-f3en4w\",\n",
    ")\n",
    "\n",
    "con_mysql = wr.data_api.rds.connect(\n",
    "    resource_arn=\"arn:aws:rds:us-east-1:111111111111:cluster:mysql-serverless-cluster-wrangler\",\n",
    "    database=\"test_rds\",\n",
    "    secret_arn=\"arn:aws:secretsmanager:us-east-1:111111111111:secret:aws-sdk-pandas/mysql-23df3\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read from database\n",
    "- [wr.data_api.redshift.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.data_api.redshift.read_sql_query.html)\n",
    "- [wr.data_api.rds.read_sql_query()](https://aws-sdk-pandas.readthedocs.io/en/3.14.0/stubs/awswrangler.data_api.rds.read_sql_query.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = wr.data_api.redshift.read_sql_query(\n",
    "    sql=\"SELECT * FROM public.test_table\",\n",
    "    con=con_redshift,\n",
    ")\n",
    "\n",
    "df = wr.data_api.rds.read_sql_query(\n",
    "    sql=\"SELECT * FROM test.test_table\",\n",
    "    con=con_rds,\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.14",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
